package com.jiuyuejiu.func_pic;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.jiuyuejiu.application.ConstValues;
import com.kylin.core.dialog.ApkDownloadDialog;
import com.kylin.core.net.RestClient;
import com.kylin.core.net.callback.IError;
import com.kylin.core.net.callback.IFailure;
import com.kylin.core.net.callback.ISuccess;
import com.kylin.core.net.callback.OnDownLoadProgress;
import com.kylin.core.utils.dbtutil.DbtLog;


/**
 *
 */
public class PicService {

    private final String TAG = "MainService";

    private Context context;
    private Handler handler;

    /**
     *
     */
    public PicService() {

    }

    public PicService(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    // 获取Banner
    public void getBannerPicList(String url) {

        RestClient.builder()
                //.url(PropertiesUtil.getProperties("platform_ip"))
                .url(url)
                // .params("", "")
                //.params("data", jsonZip)
                .loader(context)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (!"".equals(response)) {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", response);
                            bundle.putString("status", ConstValues.SUCCESS);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT0;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", "数据返回异常");
                            bundle.putString("status", ConstValues.ERROR);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT0;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                            DbtLog.logUtils("返回数据异常R", response);
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", msg2);
                        bundle.putString("status", ConstValues.ERROR);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT0;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", "请检查您的网络");
                        bundle.putString("status", ConstValues.EXCEPTION);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT0;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .builde()
                .get();

    }


    // 获取所有图片记录
    public void getAllPicRecord(String url) {


        RestClient.builder()
                //.url(PropertiesUtil.getProperties("platform_ip"))
                .url(url)
                // .params("", "")
                //.params("data", jsonZip)
                .loader(context)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (!"".equals(response)) {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", response);
                            bundle.putString("status", ConstValues.SUCCESS);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT1;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", "数据返回异常");
                            bundle.putString("status", ConstValues.ERROR);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT1;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                            DbtLog.logUtils("返回数据异常R", response);
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", msg2);
                        bundle.putString("status", ConstValues.ERROR);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT1;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", "请检查您的网络");
                        bundle.putString("status", ConstValues.EXCEPTION);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT1;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .builde()
                .get();

    }

    // 获取所有图片类型
    public void getPicTypeList(String url) {

        RestClient.builder()
                //.url(PropertiesUtil.getProperties("platform_ip"))
                .url(url)
                // .params("", "")
                //.params("data", jsonZip)
                .loader(context)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (!"".equals(response)) {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", response);
                            bundle.putString("status", ConstValues.SUCCESS);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT2;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString("formjson", "数据返回异常");
                            bundle.putString("status", ConstValues.ERROR);
                            if (handler != null) {
                                Message msg = new Message();
                                msg.what = ConstValues.WAIT2;
                                msg.setData(bundle);
                                handler.sendMessage(msg);
                            }
                            DbtLog.logUtils("返回数据异常R", response);
                        }
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", msg2);
                        bundle.putString("status", ConstValues.ERROR);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT2;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Bundle bundle = new Bundle();
                        bundle.putString("formjson", "请检查您的网络");
                        bundle.putString("status", ConstValues.EXCEPTION);
                        if (handler != null) {
                            Message msg = new Message();
                            msg.what = ConstValues.WAIT2;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                })
                .builde()
                .get();

    }

}
